﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LinWon.Core.Model
{
    /// <summary>
    /// 会员
    /// </summary>
    public class Member
    {
        #region
        private int _autoid;
        /// <summary>
        /// 会员号
        /// </summary>
        public int AutoID
        {
            get { return _autoid; }
            set { _autoid = value; }
        }
        private int _regionid;
        /// <summary>
        /// 区域号
        /// </summary>
        public int RegionID
        {
            get { return _regionid; }
            set { _regionid = value; }
        }
        private string _name = string.Empty;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return _name; }
            set 
            {
                if(Validata.GetLengthByByte(value)>50)
                    throw new LwNormalException("会员名称太长，最多25个中文或50个英文");
                _name = value; }
        }
        private DateTime _createtime;
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime
        {
            get { return _createtime; }
            set { _createtime = value; }
        }
        private DateTime _endtime;
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime
        {
            get { return _endtime; }
            set { _endtime = value; }
        }
        private MemberState _state;
        /// <summary>
        /// 状态
        /// </summary>
        public MemberState State
        {
            get { return _state; }
            set { _state = value; }
        }
        private bool _isadmin;
        /// <summary>
        /// 是否管理员
        /// </summary>
        public bool IsAdmin
        {
            get { return _isadmin; }
            set { _isadmin = value; }
        }
        private int _maxuser;
        /// <summary>
        /// 最大用户数
        /// </summary>
        public int MaxUser
        {
            get { return _maxuser; }
            set { _maxuser = value; }
        }

        private string _website = string.Empty;
        /// <summary>
        /// 网站地址
        /// </summary>
        public string WebSite
        {
            get { return _website; }
            set {
                if (Validata.GetLengthByByte(value) > 255)
                    throw new LwNormalException("网址太长，最多输入255个字");
                _website = value; }
        }
        private string _email = string.Empty;

        public string Email
        {
            get { return _email; }
            set {
                if (Validata.GetLengthByByte(value) > 50)
                    throw new LwNormalException("Email太长，最多输入50个字");
                _email = value; }
        }
        private string _tel = string.Empty;
        /// <summary>
        /// 联系电话
        /// </summary>
        public string Tel
        {
            get { return _tel; }
            set {
                if (Validata.GetLengthByByte(value) > 50)
                    throw new LwNormalException("联系电话太长，最多输入50个字");
                _tel = value; }
        }
        private string _fax = string.Empty;
        /// <summary>
        /// 传真
        /// </summary>
        public string Fax
        {
            get { return _fax; }
            set {
                if (Validata.GetLengthByByte(value) > 20)
                    throw new LwNormalException("传真号码太长，最多输入20个字");
                _fax = value; }
        }
        private string _address = string.Empty;
        /// <summary>
        /// 联系地址
        /// </summary>
        public string Address
        {
            get { return _address; }
            set {
                if (Validata.GetLengthByByte(value) > 200)
                    throw new LwNormalException("联系地址太长，最多输入200个字");
                _address = value; }
        }
        private string _zipcode = string.Empty;
        /// <summary>
        /// 邮编
        /// </summary>
        public string ZipCode
        {
            get { return _zipcode; }
            set {
                if (Validata.GetLengthByByte(value) > 10)
                    throw new LwNormalException("邮编太长，最多输入10个字");
                _zipcode = value; }
        }
        private string _description = string.Empty;
        /// <summary>
        /// 公司描述
        /// </summary>
        public string Description
        {
            get { return _description; }
            set {
                if (Validata.GetLengthByByte(value) > 500)
                    throw new LwNormalException("公司描述太长，最多输入500个字");
                _description = value; }
        }
        private string _logourl = string.Empty;
        /// <summary>
        /// logo
        /// </summary>
        public string LogoURL
        {
            get { return _logourl; }
            set {
                if (Validata.GetLengthByByte(value) > 255)
                    throw new LwNormalException("Logo地址太长，最多输入255个字");
                _logourl = value; }
        }
        #endregion

        #region static sql string
        private const string select = "SELECT AutoID, RegionID, [Name], CreateTime, EndTime, [State], IsAdmin, MaxUser, WebSite, "
                                    + "Email, Tel, Fax, [Address], ZipCode, [Description], LogoURL "
                                    + "FROM vMemberInfo WHERE [AutoID]=@AutoID";
        private const string insert = "pRegMember";
        private const string upMember = "UPDATE LW_Member SET RegionID=@Region,[Name]=@Name,EndTime=@EndTime,[State]=@State,"
                                    + "MaxUser=@MaxUser WHERE AutoID=@AutoID";
        private const string upMemInfo = "UPDATE LW_MemberInfo SET WebSite=@WebSite,Email=@Email,Tel=@Tel,Fax=@Fax,[Address]=@Address,"
                                    + "ZipCode=@ZipCode,[Description]=@Description,LogoURL=@LogoURL WHERE MemberID=@MemberID";
        private const string delete = "pDeleteMember";
        #endregion

        #region
        /// <summary>
        /// 会员
        /// </summary>
        public Member(){}

        /// <summary>
        /// 会员
        /// </summary>
        /// <param name="id">会员号</param>
        public Member(int id)
        {  
            System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@AutoID", System.Data.SqlDbType.Int);
            p.Value = id;

            Data.SqlHelper helper = Data.DataFactory.GetHelper();
            System.Data.SqlClient.SqlDataReader dr = null;

            try
            {
                dr = helper.ExecuteReader(select, p);
                #region 按SELECT顺序,填充数据
                if (dr.Read())
                {
                    //int index = 0;
                    //this.AutoID = dr.GetInt32(index++);
                    //this.RegionID = dr.GetInt32(index++);
                    //this.Name = dr.GetString(index++);
                    //this.CreateTime = dr.GetDateTime(index++);
                    //if (!dr.IsDBNull(index++)) this.EndTime = dr.GetDateTime(index);
                    //this.State = (MemberState)dr.GetInt32(index++);
                    //this.IsAdmin = dr.GetBoolean(index++);
                    //this.MaxUser = dr.GetInt32(index++);
                    //if (!dr.IsDBNull(index++)) this.WebSite = dr.GetString(index);
                    //if (!dr.IsDBNull(index++)) this.Email = dr.GetString(index);
                    //if (!dr.IsDBNull(index++)) this.Tel = dr.GetString(index);
                    //if (!dr.IsDBNull(index++)) this.Fax = dr.GetString(index);
                    //if (!dr.IsDBNull(index++)) this.Address = dr.GetString(index);
                    //if (!dr.IsDBNull(index++)) this.ZipCode = dr.GetString(index);
                    //if (!dr.IsDBNull(index++)) this.Description = dr.GetString(index);
                    //if (!dr.IsDBNull(index++)) this.LogoURL = dr.GetString(index);

                    this.FillByDataReader(dr);
                }
                #endregion
            }
            finally
            {
                if (null != dr)
                    dr.Close();
            }
        }

        /// <summary>
        /// 删除当前会员
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            if (this.AutoID > 0)
            {
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@MemberID", System.Data.SqlDbType.Int, 4);
                p.Value = this.AutoID;

                Data.SqlHelper helper = Data.DataFactory.GetHelper();

                return helper.ExecuteNonQuery(delete, p);
            }
            else
                throw new LwNormalException("删除失败，未指定会员编号");
        }

        /// <summary>
        /// 用DataReader填充数据，SELECT必须按顺序(不会打开或关闭dr)
        /// </summary>
        /// <param name="dr"></param>
        public void FillByDataReader(System.Data.SqlClient.SqlDataReader dr)
        {
            int index = 0;
            this.AutoID = dr.GetInt32(index++);
            this.RegionID = dr.GetInt32(index++);
            this.Name = dr.GetString(index++);
            this.CreateTime = dr.GetDateTime(index++);
            if (!dr.IsDBNull(index++)) this.EndTime = dr.GetDateTime(index);
            this.State = (MemberState)dr.GetInt32(index++);
            this.IsAdmin = dr.GetBoolean(index++);
            this.MaxUser = dr.GetInt32(index++);
            if (!dr.IsDBNull(index)) this.WebSite = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Email = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Tel = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Fax = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Address = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.ZipCode = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Description = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.LogoURL = dr.GetString(index);
        }

        /// <summary>
        /// 添加会员，返回会员号
        /// </summary>
        /// <returns></returns>
        public int Insert()
        {
            #region 创建参数
            System.Data.SqlClient.SqlParameter[] parms = {
                new System.Data.SqlClient.SqlParameter("@RegionID",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@Name",System.Data.SqlDbType.VarChar,50),
                new System.Data.SqlClient.SqlParameter("@EndTime",System.Data.SqlDbType.DateTime),
                new System.Data.SqlClient.SqlParameter("@State",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@WebSite",System.Data.SqlDbType.VarChar,255),
                new System.Data.SqlClient.SqlParameter("@Email",System.Data.SqlDbType.VarChar,50),
                new System.Data.SqlClient.SqlParameter("@Tel",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Fax",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Address",System.Data.SqlDbType.VarChar,200),
                new System.Data.SqlClient.SqlParameter("@ZipCode",System.Data.SqlDbType.VarChar,10),
                new System.Data.SqlClient.SqlParameter("@Description",System.Data.SqlDbType.VarChar,500),                
                new System.Data.SqlClient.SqlParameter("@LogoURL",System.Data.SqlDbType.VarChar,255)};

            int pIndex = 0;
            parms[pIndex++].Value = this.RegionID;
            parms[pIndex++].Value = this.Name;

            if (this.EndTime == DateTime.MinValue)
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.EndTime;

            parms[pIndex++].Value = this.State;

            if (string.IsNullOrEmpty(this.WebSite))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.WebSite;

            if (string.IsNullOrEmpty(this.Email))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.Email;

            if (string.IsNullOrEmpty(this.Tel))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.Tel;

            if (string.IsNullOrEmpty(this.Fax))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.Fax;

            if (string.IsNullOrEmpty(this.Address))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.Address;

            if (string.IsNullOrEmpty(this.ZipCode))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.ZipCode;

            if (string.IsNullOrEmpty(this.Description))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.Description;

            if (string.IsNullOrEmpty(this.LogoURL))
                parms[pIndex++].Value = DBNull.Value;
            else
                parms[pIndex++].Value = this.LogoURL;
            #endregion

            Data.SqlHelper helper = Data.DataFactory.GetHelper();
            int result = helper.RunProcReturnInt(insert, parms);
            this.AutoID = result;
            return result;
        }

        /// <summary>
        /// 修改会员信息
        /// </summary>
        /// <returns></returns>
        public int Modify()
        {
            System.Data.SqlClient.SqlParameter[] memParms ={ 
                new System.Data.SqlClient.SqlParameter("@AutoID",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@Region",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@Name",System.Data.SqlDbType.VarChar,50),
                new System.Data.SqlClient.SqlParameter("@EndTime",System.Data.SqlDbType.DateTime),
                new System.Data.SqlClient.SqlParameter("@State",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@MaxUser",System.Data.SqlDbType.Int,4)};

            int index = 0;

            memParms[index++].Value = this.AutoID;
            memParms[index++].Value = this.RegionID;
            memParms[index++].Value = this.Name;

            if (this.EndTime == DateTime.MinValue)
                memParms[index++].Value = DBNull.Value;
            else
                memParms[index++].Value = this.EndTime;

            memParms[index++].Value = this.State;
            memParms[index++].Value = this.MaxUser;

            System.Data.SqlClient.SqlParameter[] infoParms ={ 
                new System.Data.SqlClient.SqlParameter("@MemberID",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@WebSite",System.Data.SqlDbType.VarChar,255),
                new System.Data.SqlClient.SqlParameter("@Email",System.Data.SqlDbType.VarChar,50),
                new System.Data.SqlClient.SqlParameter("@Tel",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Fax",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Address",System.Data.SqlDbType.VarChar,200),
                new System.Data.SqlClient.SqlParameter("@ZipCode",System.Data.SqlDbType.VarChar,10),
                new System.Data.SqlClient.SqlParameter("@Description",System.Data.SqlDbType.VarChar,500),                
                new System.Data.SqlClient.SqlParameter("@LogoURL",System.Data.SqlDbType.VarChar,255)};

            index = 0;

            infoParms[index++].Value = this.AutoID;

            if (string.IsNullOrEmpty(this.WebSite))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.WebSite;

            if (string.IsNullOrEmpty(this.Email))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.Email;

            if (string.IsNullOrEmpty(this.Tel))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.Tel;

            if (string.IsNullOrEmpty(this.Fax))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.Fax;

            if (string.IsNullOrEmpty(this.Address))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.Address;

            if (string.IsNullOrEmpty(this.ZipCode))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.ZipCode;

            if (string.IsNullOrEmpty(this.Description))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.Description;

            if (string.IsNullOrEmpty(this.LogoURL))
                infoParms[index++].Value = DBNull.Value;
            else
                infoParms[index++].Value = this.LogoURL;

            Data.SqlHelper helper = Data.DataFactory.GetHelper();
            
            helper.ExecuteNonQuery(upMember, memParms);
            helper.ExecuteNonQuery(upMemInfo, infoParms);

            return 1;
        }
        #endregion
    }

    /// <summary>
    /// 会员状态
    /// </summary>
    public enum MemberState
    {
        /// <summary>
        /// 未激活
        /// </summary>
        NotActivated,
        /// <summary>
        /// 正常
        /// </summary>
        Normal,
        /// <summary>
        /// 禁用
        /// </summary>
        Disabled,
        /// <summary>
        /// 所有(仅查询时用到)
        /// </summary>
        All
    }
}
